/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.futco.sysconthos.Modelo.Admisiones.Daos;

import org.futco.sysconthos.Modelo.Admisiones.Usuario;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author casher
 */
public class UsuarioDAO implements IDAO<Usuario> {

  private Model model;
    private PreparedStatement psInsert;

    public UsuarioDAO() throws SQLException  {
        model = Model.getInstance();
    }

      public void insert(Usuario t) throws SQLException {
       if (psInsert == null) {
            String sqlInsert = "INSERT INTO usuario ( user,password,nombres,apellidos,direccion,id,tel,email,sexo ) VALUES (?,?,?,?,?,?,?,?,?)";
            psInsert = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);


            

            psInsert.setString(1, t.getUsuario());
            psInsert.setString(2, t.getPassword());
            psInsert.setString(3, t.getNombre());
            psInsert.setString(4, t.getApellido());
            psInsert.setString(5, t.getDireccion());
            psInsert.setString(6, t.getId());
            psInsert.setString(7, t.getTel());
            psInsert.setString(8, t.getEmail());
            psInsert.setString(9, t.getSexo());
          

            psInsert.executeUpdate();
        }
      }

      public Usuario findUserPass(String u,String p) throws SQLException {
      java.sql.PreparedStatement pst =  null;
      Usuario us = new Usuario();
      ResultSet rs = null;
      pst = model.getCon().prepareStatement("Select * from usuario where  user = ? and  password = ?");
      pst.setString(1, u);
      pst.setString(2, p);
      rs = pst.executeQuery();
      if (rs.next()) {

       us.setUsuario(rs.getString(1));
       us.setPassword(rs.getString(2));
 
        }
        if (pst != null) {
            pst.close();
        }
        if (rs != null) {
            rs.close();
        }
        return us;
    }

      public Usuario findUser(String u) throws SQLException {
          java.sql.PreparedStatement pst =  null;
          Usuario us = new Usuario();
          ResultSet rs = null;
          pst = model.getCon().prepareStatement("Select * from usuario where  nombres = ?");
          pst.setString(1, u);
      
          rs = pst.executeQuery();
          if (rs.next()) {

           us.setNombre(rs.getString(1));
        
     
            }
            if (pst != null) {
                pst.close();
            }
            if (rs != null) {
                rs.close();
            }
            return us;
        }
      public Usuario findUserByID(String id) throws SQLException {
          java.sql.PreparedStatement pst =  null;
          Usuario us = new Usuario();
          ResultSet rs = null;
          pst = model.getCon().prepareStatement("Select * from usuario where  id = ?");
          pst.setString(1, id);
      
          rs = pst.executeQuery();
          if (rs.next()) {

           us.setUsuario(rs.getString(1));
           us.setPassword(rs.getString(2));
           us.setNombre(rs.getString(3));
           us.setApellido(rs.getString(4));
           us.setDireccion(rs.getString(5));
           us.setId(rs.getString(6));
           us.setTel(rs.getString(7));
           us.setEmail(rs.getString(8));
           us.setSexo(rs.getString(9));
           
  
            }
            if (pst != null) {
                pst.close();
            }
            if (rs != null) {
                rs.close();
            }
            return us;
        }
      public void update(String identifi, Usuario u) throws SQLException {

  		String sqlInsert = "UPDATE usuario SET  user = ?,password = ?, nombres = ?, apellidos = ?, "
  				+ "direccion = ?,  tel = ?, email = ?,  sexo = ? WHERE id = ?";
  		psInsert = (PreparedStatement) model.getCon().prepareStatement(
  				sqlInsert);

  		psInsert.setString(1, u.getUsuario());
  		psInsert.setString(2, u.getPassword());
  		psInsert.setString(3, u.getNombre());
  		psInsert.setString(4, u.getApellido());
  		psInsert.setString(5, u.getDireccion());
  		psInsert.setString(6, u.getTel());
  		psInsert.setString(7, u.getEmail());
  		psInsert.setString(8, u.getSexo());
  		psInsert.setString(9, identifi);
  		psInsert.executeUpdate();

  	}

	@Override
	public void delete(Usuario t) throws SQLException {
		// TODO Auto-generated method stub
		
	}
	private Usuario loadUser(ResultSet rs) throws SQLException {
		Usuario user = new Usuario();

		
		user.setId(rs.getString("id"));
		user.setNombre(rs.getString("nombres"));
		user.setApellido(rs.getString("apellidos"));
		user.setSexo(rs.getString("sexo"));
		
		user.setDireccion(rs.getString("direccion"));
		user.setTel(rs.getString("tel"));
		user.setEmail(rs.getString("email"));

		return user;
	}
	
	

	
	
	
	
	
	
	@Override
	public List<Usuario> getAll() throws SQLException {
		Usuario u = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List lista = new LinkedList();

		try {
			String sqlInsert = "Select * from usuario";
			ps = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);
			rs = ps.executeQuery();
			while (rs.next()) {
				u = this.loadUser(rs);
				lista.add(u);
			}

		} finally {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
		}
		return lista;
	}

	@Override
	public List<Usuario> getAllBy(String t, String n) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Usuario find(Integer id) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
